package com.ptszyxx.popose.module.base.user;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.constants.BundleConstant;
import com.ptszyxx.popose.databinding.ActivityUserSexBinding;
import com.ptszyxx.popose.databinding.ActivityUserTagBinding;
import com.ptszyxx.popose.module.base.user.vm.UserSexVM;
import com.ptszyxx.popose.module.base.user.vm.UserTagVM;
import com.ysg.base.BaseFragment;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.http.data.entity.user.LabelItemEntity;
import com.ysg.http.data.entity.user.UserLabelResult;
import com.ysg.utils.YCollectionUtil;
import com.ysg.utils.YStringUtil;
import com.ysg.widget.flow.TagFlowLayout;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 完善用户信息
 */
public class UserTagFragment extends BaseFragment<ActivityUserTagBinding, UserTagVM> {

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.activity_user_tag;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public UserTagVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(UserTagVM.class);
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.requestList();
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onRefreshEvent.observe(this, o -> setTagUI());
    }

    private void setTagUI() {
        List<String> list = new ArrayList<>();
        Set<Integer> checkIds = new HashSet<>();
        for (int i = 0; i < viewModel.tagList.size(); i++) {
            LabelItemEntity item = viewModel.tagList.get(i);
            if (YStringUtil.eq(1, item.getIsselect())){
                checkIds.add(i);
            }
            list.add(item.getName());
        }
        binding.flowlayout.setData(list, checkIds);
        binding.flowlayout.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {
                String ids = "";
                for (Integer i: selectPosSet){
                    LabelItemEntity item = viewModel.tagList.get(i);
                    ids += item.getId() + ",";
                }
                viewModel.ids = YStringUtil.clearEndComma(ids);
            }
        });
    }

}